﻿using System;
using System.Text;
using TI_BLE_HCI_ClientLib.GenericHCIEntities;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;

namespace TI_BLE_HCI_ClientLib.HCIEvents
{
    public class HCIEvent_ATT_Error_Response : HCIEvent_GATT_Base_Event
    {
        public byte RequestOpCode
        {
            get
            {
                if (base.PduLen < 1) throw new ArgumentException("PduLen<1");

                return base.Pdu[0];
            }
        }

        public ushort AttributeHandle
        {
            get
            {
                if (base.PduLen < 3) throw new ArgumentException("PduLen<3");

                return (ushort)(base.Pdu[1] + (base.Pdu[2] << 8));
            }
        }

        public ErrorCodeEnum ErrorCode
        {
            get
            {
                if (base.PduLen < 4) throw new ArgumentException("PduLen<4");

                return (ErrorCodeEnum)base.Pdu[3];
            }
        }

        public enum ErrorCodeEnum : byte
        {
            AttErrInvalidHandle = 0x01, //!< Attribute handle value given was not valid on this attribute server
            AttErrReadNotPermitted = 0x02, //!< Attribute cannot be read
            AttErrWriteNotPermitted = 0x03, //!< Attribute cannot be written
            AttErrInvalidPdu = 0x04, //!< The attribute PDU was invalid
            AttErrInsufficientAuthen = 0x05, //!< The attribute requires authentication before it can be read or written
            AttErrUnsupportedReq = 0x06, //!< Attribute server doesn't support the request received from the attribute client
            AttErrInvalidOffset = 0x07, //!< Offset specified was past the end of the attribute
            AttErrInsufficientAuthor = 0x08, //!< The attribute requires an authorization before it can be read or written
            AttErrPrepareQueueFull = 0x09, //!< Too many prepare writes have been queued
            AttErrAttrNotFound = 0x0a, //!< No attribute found within the given attribute handle range
            AttErrAttrNotLong = 0x0b, //!< Attribute cannot be read or written using the Read Blob Request or Prepare Write Request
            AttErrInsufficientKeySize = 0x0c, //!< The Encryption Key Size used for encrypting this link is insufficient
            AttErrInvalidValueSize = 0x0d, //!< The attribute value length is invalid for the operation
            AttErrUnlikely = 0x0e, //!< The attribute request that was requested has encountered an error that was very unlikely, and therefore could not be completed as requested
            AttErrInsufficientEncrypt = 0x0f, //!< The attribute requires encryption before it can be read or written
            AttErrUnsupportedGrpType = 0x10, //!< The attribute type is not a supported grouping attribute as defined by a higher layer specification
            AttErrInsufficientResources = 0x11, //!< Insufficient Resources to complete the request
            AttErrInvalidValue = 0x80, //!< The attribute value is invalid for the operation
        }

        public HCIEvent_ATT_Error_Response(HCIVendorSpecificEventHostErrorCode status,
            ushort connHandle, byte pduLen, byte[] pdu)
            : base(status, connHandle, pduLen, pdu)
        {
        }
    }
}
